package com.mia.props.network;

import com.mia.props.common.entities.TileDecobench;
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
import io.netty.buffer.ByteBuf;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.tileentity.TileEntity;

public class Msg0x01SelectDeco<T extends Msg0x01SelectDeco> implements IMessage, IMessageHandler<T, Msg0x01SelectDeco> {
   public int x;
   public int y;
   public int z;
   public int decoID;
   public boolean shift;

   public Msg0x01SelectDeco() {
   }

   public Msg0x01SelectDeco(TileDecobench tile, int decoID, boolean shift) {
      this.x = tile.xCoord;
      this.y = tile.yCoord;
      this.z = tile.zCoord;
      this.decoID = decoID;
      this.shift = shift;
   }

   public void fromBytes(ByteBuf buf) {
      this.x = buf.readInt();
      this.y = buf.readInt();
      this.z = buf.readInt();
      this.decoID = buf.readShort();
      this.shift = buf.readBoolean();
   }

   public void toBytes(ByteBuf buf) {
      buf.writeInt(this.x);
      buf.writeInt(this.y);
      buf.writeInt(this.z);
      buf.writeShort(this.decoID);
      buf.writeBoolean(this.shift);
   }

   public Msg0x01SelectDeco onMessage(Msg0x01SelectDeco msg, MessageContext ctx) {
      EntityPlayerMP player = ctx.getServerHandler().playerEntity;
      TileEntity te = player.worldObj.getTileEntity(msg.x, msg.y, msg.z);
      if (te instanceof TileDecobench && !te.isInvalid()) {
         ((TileDecobench)te).onData(player, msg.decoID, msg.shift);
      }

      return null;
   }
}
